/*
@title: Lost Alien
@description: "Lost Alien" is a maze-themed game where players guide a stranded alien back to its spaceship. Navigate through challenging mazes filled with obstacles and portals to reach your destination. The game involves strategic puzzle-solving to overcome barriers and find the correct path.
@author: Joseph Smesny
@tags: []
@addedOn: 2024-05-20
@img: ""
*/

const song = tune`
201.34228187919464: C4-201.34228187919464,
201.34228187919464: D5/201.34228187919464 + A4/201.34228187919464 + E4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464,
201.34228187919464: E5/201.34228187919464 + B4/201.34228187919464 + F4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: A4/201.34228187919464 + D5/201.34228187919464 + E4^201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C5/201.34228187919464 + G4/201.34228187919464 + D4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: D5/201.34228187919464 + A4/201.34228187919464 + E4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464,
201.34228187919464: E5/201.34228187919464 + B4/201.34228187919464 + F4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: D5/201.34228187919464 + A4/201.34228187919464 + E4^201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C5/201.34228187919464 + G4/201.34228187919464 + D4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: D5/201.34228187919464 + A4/201.34228187919464 + E4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464,
201.34228187919464: E5/201.34228187919464 + B4/201.34228187919464 + F4^201.34228187919464 + C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: C4-201.34228187919464,
201.34228187919464: D5/201.34228187919464 + A4/201.34228187919464 + E4^201.34228187919464`
const playback = playTune (song, Infinity)
const win = tune`
205.4794520547945: E4-205.4794520547945 + E5~205.4794520547945 + F5/205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D4-205.4794520547945,
205.4794520547945: E5~205.4794520547945 + F5/205.4794520547945 + C4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D5~205.4794520547945 + C4-205.4794520547945,
205.4794520547945: C5~205.4794520547945 + F5/205.4794520547945 + D4-205.4794520547945 + A4-205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D5~205.4794520547945 + E4-205.4794520547945,
205.4794520547945: E5~205.4794520547945 + A4^205.4794520547945 + F5/205.4794520547945 + F4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + B4^205.4794520547945 + G4-205.4794520547945,
205.4794520547945: B4^205.4794520547945 + F5/205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + E5~205.4794520547945 + A4^205.4794520547945 + F4-205.4794520547945,
205.4794520547945: D5~205.4794520547945 + F5/205.4794520547945 + E4-205.4794520547945 + C4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + C5~205.4794520547945 + D4-205.4794520547945 + A4-205.4794520547945 + G4-205.4794520547945,
205.4794520547945: D5~205.4794520547945 + F5/205.4794520547945 + C4-205.4794520547945 + E4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + E5~205.4794520547945 + A4^205.4794520547945 + F4-205.4794520547945,
205.4794520547945: B4^205.4794520547945 + F5/205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + B4^205.4794520547945 + G4-205.4794520547945,
205.4794520547945: E5~205.4794520547945 + A4^205.4794520547945 + F5/205.4794520547945 + F4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D5~205.4794520547945 + E4-205.4794520547945 + C4-205.4794520547945,
205.4794520547945: C5~205.4794520547945 + F5/205.4794520547945 + D4-205.4794520547945 + A4-205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D5~205.4794520547945 + C4-205.4794520547945 + E4-205.4794520547945,
205.4794520547945: E5~205.4794520547945 + A4^205.4794520547945 + F5/205.4794520547945 + F4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + B4^205.4794520547945 + G4-205.4794520547945,
205.4794520547945: B4^205.4794520547945 + F5/205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + E5~205.4794520547945 + A4^205.4794520547945 + F4-205.4794520547945,
205.4794520547945: D5~205.4794520547945 + F5/205.4794520547945 + E4-205.4794520547945 + C4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + C5~205.4794520547945 + D4-205.4794520547945 + A4-205.4794520547945 + G4-205.4794520547945,
205.4794520547945: D5~205.4794520547945 + F5/205.4794520547945 + C4-205.4794520547945 + E4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + E5~205.4794520547945 + A4^205.4794520547945 + F4-205.4794520547945,
205.4794520547945: B4^205.4794520547945 + F5/205.4794520547945 + G4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + B4^205.4794520547945 + G4-205.4794520547945,
205.4794520547945: E5~205.4794520547945 + A4^205.4794520547945 + F5/205.4794520547945 + F4-205.4794520547945,
205.4794520547945: G5-205.4794520547945 + D5~205.4794520547945 + E4-205.4794520547945`
const goal = "k"
const goaltwo = "u"
const player = "p"
const tile = "l"
const tiletwo = "f"
const playertwo = "r"
const wall = "w"
const ptwo= "t"
const ptwothree= "m"
const portal = "x"
const portaltwo = "Q"
const crate = "c"
setLegend(
   [ player, bitmap`
................
......444.......
......444.......
......040.......
......444.......
.......4........
.......4........
....4444444.....
....4.444.4.....
....4.444.4.....
....4.444.4.....
....4.444.4.....
......444.......
......4.4.......
......4.4.......
.....44.44......` ],
   [portal,bitmap`
0000000000000000
0557777777777550
0577555555557750
0775555555555770
0755577777755570
0755777557775570
0755775555775570
0755755555575570
0755755555575570
0755755555575570
0755775555775570
0755777557775570
0755577777755570
0575555555555750
0557555555557550
0000000000000000`],
   [ptwo,bitmap`
0000000000000000
0557777777777550
0577555555557750
0775555555555770
0755577777755570
0755777557775570
0755775555775570
0755755775575570
0755755775575570
0755755775575570
0755775555775570
0755777557775570
0755577777755570
0575555555555750
0557555555557550
0000000000000000`],
   [portaltwo,bitmap`
000HLLLLLLLLH000
0557777777777550
0577555555557750
H77555555555577H
L75557777775557L
L75577755777557L
L75577555577557L
L75575555557557L
L75575555557557L
L75575555557557L
L75577555577557L
L75577755777557L
H75557777775557H
0575555555555750
0557555555557550
000HLLLLLLLLH000`],
   [ptwothree,bitmap`
000HLLLLLLLLH000
0557777777777550
0577555555557750
H77555555555577H
L75557777775557L
L75577755777557L
L75577555577557L
L75575577557557L
L75575577557557L
L75575577557557L
L75577555577557L
L75577755777557L
H75557777775557H
0575555555555750
0557555555557550
000HLLLLLLLLH000`],
   [ playertwo, bitmap`
000HLLLLLLLLH000
0LLLLL444LLLLLL0
0LLLLL444LLLLLL0
HLLLLL040LLLLLLH
LLLLLL444LLLLLLL
LLLLLLL4LLLLLLLL
LLLLLLL4LLLLLLLL
LLLL4444444LLLLL
LLLL4L444L4LLLLL
LLLL4L444L4LLLLL
LLLL4L444L4LLLLL
LLLL4L444L4LLLLL
HLLLLL444LLLLLLH
0LLLLL4L4LLLLLL0
0LLLLL4L4LLLLLL0
000HL44L44LLH000` ],
   [ wall, bitmap`
0000000000000000
0990LL0990LL0990
0990LL0990LL0990
0990LL0990LL0000
0990LL0990LLLLL0
0990LL0990LLLLL0
0990LL0990000000
0990LL0999999990
0990LL0999999990
0990LL0000000000
0990LLLLLLLLLLL0
0990LLLLLLLLLLL0
0990000000000000
0999999999999990
0999999999999990
0000000000000000` ],
   [ goal, bitmap`
.......3........
......333.......
.....33333......
.....11111......
.....22222......
.....22222......
.....22222......
....3223223.....
...332232233....
...332232233....
..33311311333...
......222.......
......999.......
.....999........
......999.......
.......9........` ],
   [ goaltwo, bitmap`
000HLLL3LLLLH000
0LLLLL333LLLLLL0
0LLLL33333LLLLL0
HLLLL11111LLLLLH
LLLLL22222LLLLLL
LLLLL22222LLLLLL
LLLLL22222LLLLLL
LLLL3223223LLLLL
LLL332232233LLLL
LLL332232233LLLL
LL33311311333LLL
LLLLLL222LLLLLLL
HLLLLL999LLLLLLH
0LLLL999LLLLLLL0
0LLLLL999LLLLLL0
000HLLL9LLLLH000` ],
     [ tile, bitmap`
0000000000000000
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0111111111111110
0000000000000000` ],
   [ tiletwo, bitmap`
000HLLLLLLLLH000
0LLLLLLLLLLLLLL0
0LLLLLLLLLLLLLL0
HLLLLLLLLLLLLLLH
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
HLLLLLLLLLLLLLLH
0LLLLLLLLLLLLLL0
0LLLLLLLLLLLLLL0
000HLLLLLLLLH000` ],
  [crate, bitmap`
0000000004444444
0LLLLLLLLLLL4444
0LLLLL000LLLL444
0LLLL00600LLL440
0LLL0066600LL440
0LLL0660660LL440
0LL006606600L440
0LL066606660LL40
0L00666066600L40
0L06666666660L40
0L06666066660LL0
0L06666666660LL0
0L00000000000LL4
44LLLLLLLLLLLL44
4444LLLLLLLLL444
4444440000004444`]
)
setBackground(tile);
setSolids([player,crate,wall])
let level = 0
const levels = [
   map`
ffffffffff
ffffffffff
ffffffffff
uffffffffu
rffffffffr
Qffffffffm
cffffffffc
ffffffffff`,
  map`
p..w.wwww.w.....wtw.
ww.w..c.www.w.w.....
...wcw.w....w.w.www.
w.ww.w.w.w.ww.w.w.w.
x.w..w...w.ww.www.w.
w...ww.www.wwww...w.
wwcww.....c..w..wwww
w.....wwwcww.w......
..wwwww...w..wwww.w.
www.....w.w.wwwk..w.
....www...w...w.w.w.
.wwwwwwwwww.wwwwwww.
....................`,
  map`
....w.w...w...www.wp
www...c.w...w.w..cw.
w...w.wwwwwww.w.w.w.
w.www.wx..w.c...w...
w.w.w.www.w.wwwcwwww
..c.w..c....w.......
w.w.wcwww.wwwww.www.
w.w.w.w.....w.....w.
w.w...wwwww.w.www.w.
w.ww........w...w...
wc.wwwwwwwwwwww.wwww
w..........kw.c....t`,
   map`
pw..c...tw...w....w.
.w..wwwwww.wwwwww.w.
.wk.w....w....w.www.
.wwwwwww.wwww.c.....
c..c.......ww.w.wwwx
.wwwwwcww...w.w.wwww
www.....wcw.wcw....w
www.ww.ww.www.wcww.w
....w...w.....w.w..w
.wwww.w.w.wwwww.w..w
......c.....c......w
wwwwwwwwwwwwwwwwwwww`,
     map`
x.w.w.wwwww.........
..w.w.....ww.wwwcww.
.ww.w.www.c..wt...w.
....w...w...wwwww.w.
wwwcwcwwwwwcw.w.w.c.
............w.w.w.w.
.www.wwwwwwcw.w.w.w.
..c......w....w.w.w.
.w.w.www.w.w.ww.wcw.
...wcww..w.w........
wwww.ww..w.wwwwwcww.
p...c.w..wk.......w.`,
   map`
.w.ww...wwww.......w
pc....w....w.ww.ww.w
www.w.w.wwww.....c..
....c...c..c..w.ww.w
wwcwwwww.w.w.ww.w..w
x..c...w...w..w.w..w
ww.ww.ww.w.ww.wcw..w
...w..w..w..w..c....
.w.w.ww.www.wwwwwwwk
.w.w....c....w....ww
.w.wwwwww.ww.w.tw.w.
.c.........w.c..w...`,
   map`
....w.c..........w.w
.wwcw.w.w..wwww.ww..
......w.ww.wt.w.w..w
cww.www..w.ww.w.w.ww
...ww.ww.w..w.w.w..w
.w.w...c..w.w...w..w
...ww.ww....wwwwwwcw
.www...w..www.......
...w.w.wwcwc....ww.w
ww.w.w..w.w.w.w.w..w
...w.ww.w.w.w.w.c.ww
xwpw..w...c.....wk.w`,





]

setMap(levels[level])


setPushables({
  [ player]: [crate]
  
})

onInput("s", () => {
  getFirst(player).y += 1
   
})

onInput("w", () => {
  getFirst(player).y -= 1
  
})

onInput("d", () => {
  getFirst(player).x += 1
   
})

onInput("a", () => {
  getFirst(player).x -= 1
   
})

// click j to reset map
onInput("k", () => {
  const currentLevel = levels[level]; // get the original map of the levels
  // make sure the level exists before we load it
  if (currentLevel !== undefined) {
    
    setMap(currentLevel);
  }
});
afterInput(() => {
  const currentLevel = levels[level+1];
 const targetNumber = tilesWith(goal).length;
const numberCovered = tilesWith(goal, player).length;
if (numberCovered ===targetNumber) {
level = level + 1;
 if (currentLevel !== undefined) {
      setMap(currentLevel);
    } else {
      addText("YOU WIN!", { y: 4, color: color`3` });
      const win = tune`
164.83516483516485: A5^164.83516483516485 + E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485,
164.83516483516485: G5^164.83516483516485 + D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485 + F5-164.83516483516485,
164.83516483516485: F5^164.83516483516485 + C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485 + E5-164.83516483516485,
164.83516483516485: E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485 + D5-164.83516483516485,
164.83516483516485: D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485 + C5-164.83516483516485 + G4-164.83516483516485,
164.83516483516485: C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485 + B4-164.83516483516485 + F4-164.83516483516485,
164.83516483516485: B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485 + A4-164.83516483516485 + E4-164.83516483516485,
164.83516483516485: A4^164.83516483516485 + E4^164.83516483516485 + G4-164.83516483516485 + D4-164.83516483516485,
164.83516483516485: G4^164.83516483516485 + D4^164.83516483516485 + F4-164.83516483516485 + C4-164.83516483516485,
164.83516483516485: F4^164.83516483516485 + C4^164.83516483516485 + E4-164.83516483516485,
164.83516483516485: E4^164.83516483516485 + D4-164.83516483516485,
164.83516483516485: D4^164.83516483516485 + C4-164.83516483516485,
164.83516483516485: C4^164.83516483516485,
164.83516483516485: D4^164.83516483516485 + C4-164.83516483516485,
164.83516483516485: E4^164.83516483516485 + D4-164.83516483516485,
164.83516483516485: F4^164.83516483516485 + C4^164.83516483516485 + E4-164.83516483516485,
164.83516483516485: G4^164.83516483516485 + D4^164.83516483516485 + F4-164.83516483516485 + C4-164.83516483516485,
164.83516483516485: A4^164.83516483516485 + E4^164.83516483516485 + G4-164.83516483516485 + D4-164.83516483516485,
164.83516483516485: B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485 + A4-164.83516483516485 + E4-164.83516483516485,
164.83516483516485: C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485 + B4-164.83516483516485 + F4-164.83516483516485,
164.83516483516485: D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485 + C5-164.83516483516485 + G4-164.83516483516485,
164.83516483516485: E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485 + D5-164.83516483516485,
164.83516483516485: F5^164.83516483516485 + C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485 + E5-164.83516483516485,
164.83516483516485: G5^164.83516483516485 + D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485 + F5-164.83516483516485,
164.83516483516485: A5^164.83516483516485 + E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485,
164.83516483516485: B5^164.83516483516485 + F5^164.83516483516485 + C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485,
164.83516483516485: G5^164.83516483516485 + D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485,
164.83516483516485: A5^164.83516483516485 + E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485,
164.83516483516485: B5^164.83516483516485 + F5^164.83516483516485 + C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485,
164.83516483516485: G5^164.83516483516485 + D5^164.83516483516485 + A4^164.83516483516485 + E4^164.83516483516485,
164.83516483516485: A5^164.83516483516485 + E5^164.83516483516485 + B4^164.83516483516485 + F4^164.83516483516485 + C4^164.83516483516485,
164.83516483516485: B5^164.83516483516485 + F5^164.83516483516485 + C5^164.83516483516485 + G4^164.83516483516485 + D4^164.83516483516485`;
      playTune (win)
    }
  }
});

afterInput(() => {
  const redPortalsCovered = tilesWith(player, portal);
  const bluePortalsCovered = tilesWith(player, ptwo);
  
  // ADDED: teleport the player to the blue portal if they are standing on the red one
  if (redPortalsCovered.length >= 1) {
    const bp = getFirst(ptwo);
    const pl = getFirst(player);

    // teleport player to blue portal
    pl.x = bp.x;
    pl.y = bp.y;
  }

  // ADDED: teleport the player to the red portal if they are standing on the blue one
  if (bluePortalsCovered.length >= 1) {
    const rp = getFirst(portal);
    const pl = getFirst(player);

    // teleport player to blue portal
    pl.x = rp.x;
    pl.y = rp.y;
  }
  
  /* your other code */
    
});

if (level == 0){
addText (" HELP! ", {
  x:7,
  y:2,
  color:color`6`
})

addText ("An alien lost its", {
  x:2,
  y:3,
  color:color`6`
})

addText ("way!", {
  x:8,
  y:4,
  color:color`6`
})

addText ("Help him return", {
  x:2,
  y:5,
  color:color`8`
})
addText ("to his ship...", {
  x:4,
  y:6,
  color:color`8`
})
addText ("Press j", {
  x:7,
  y:14,
  color:color`4`
})
  addText ("W=FWD  S=BWD", {
  x:4,
  y:7,
  color:color`3`
})
 
  addText ("A=LEFT  D=RIGHT", {
  x:3,
  y:8,
  color:color`3`
})
   addText ("Portals MIGHT", {
  x:3,
  y:12,
  color:color`6`
})
   addText ("help you!", {
  x:5,
  y:13,
  color:color`6`
})
   addText ("Push toxic crates", {
  x:2,
  y:10,
  color:color`9`
})
   addText ("out of your way", {
  x:3,
  y:11,
  color:color`9`
})
  addText ("k=Restart level", {
  x:3,
  y:9,
  color:color`7`
})
}
////////////////////////////////////////////////////////////////////////////////////////////////
onInput("j", () => {
  level+1
 clearText()
})

